package com.ruoyi.cms.blog.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.cms.blog.domain.CmsBlogTag;
import com.ruoyi.cms.blog.domain.CmsBlogType;
import com.ruoyi.cms.blog.mapper.CmsBlogTagMapper;
import com.ruoyi.cms.blog.mapper.CmsBlogTypeMapper;
import com.ruoyi.cms.comment.domain.CmsComment;
import com.ruoyi.cms.comment.service.ICmsCommentService;
import com.ruoyi.cms.tag.domain.CmsTag;
import com.ruoyi.cms.tag.mapper.CmsTagMapper;
import com.ruoyi.cms.type.domain.CmsType;
import com.ruoyi.cms.type.mapper.CmsTypeMapper;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.teach.domain.TeachStudent;
import com.ruoyi.teach.mapper.TeachStudentMapper;
import com.ruoyi.teach.service.ITeachStudentTagService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.cms.blog.mapper.CmsBlogMapper;
import com.ruoyi.cms.blog.domain.CmsBlog;
import com.ruoyi.cms.blog.service.ICmsBlogService;

/**
 * 文章管理Service业务层处理
 *
 * @author ning
 * @date 2022-01-01
 */
@Slf4j
@Service
public class CmsBlogServiceImpl implements ICmsBlogService
{
    @Autowired
    private CmsBlogMapper cmsBlogMapper;

    @Autowired
    private CmsBlogTagMapper cmsBlogTagMapper;

    @Autowired
    private CmsTagMapper cmsTagMapper;

    @Autowired
    private CmsBlogTypeMapper cmsBlogTypeMapper;

    @Autowired
    private CmsTypeMapper cmsTypeMapper;

    @Autowired
    private ICmsCommentService commentService;

    @Autowired
    private TeachStudentMapper teachStudentMapper;

    @Autowired
    private ITeachStudentTagService teachStudentTagService;

    @Override
    public int selectCmsBlogViewCount(){
        return cmsBlogMapper.selectCmsBlogViewCount();
    }

    @Override
    public int selectCmsBlogCount(){
        return cmsBlogMapper.selectCmsBlogCount();
    }


    /**
     * 查询文章管理
     *
     * @param id 文章管理主键
     * @return 文章管理
     */
    @Override
    public CmsBlog selectCmsBlogById(Long id)
    {
        CmsBlog blog = cmsBlogMapper.selectCmsBlogById(id);
        if (blog == null){
            return null;
        }
        //查询标签列表
        List<CmsBlogTag> blogTagList = cmsBlogTagMapper.selectBlogTagList(id);
        Long[] tagIds = new Long[blogTagList.size()];
        List<CmsTag> tags = new ArrayList<>();
        List<CmsType> types = new ArrayList<>();
        for (int i = 0; i<blogTagList.size();i++){
            CmsBlogTag cmsBlogTag = blogTagList.get(i);
            Long tagId = cmsBlogTag.getTagId();
            tagIds[i] = tagId;
            CmsTag cmsTag = cmsTagMapper.selectCmsTagByTagId(tagId);
            tags.add(cmsTag);
        }
        blog.setTagIds(tagIds);
        blog.setTags(tags);
        //查询分类列表
        List<CmsBlogType> blogTypeList = cmsBlogTypeMapper.selectBlogTypeList(id);
        Long[] typeIds = new Long[blogTypeList.size()];
        for (int i = 0; i<blogTypeList.size();i++){
            CmsBlogType cmsBlogType = blogTypeList.get(i);
            Long typeId = cmsBlogType.getTypeId();
            typeIds[i] = typeId;
            CmsType cmsType = cmsTypeMapper.selectCmsTypeByTypeId(typeId);
            types.add(cmsType);
        }
        blog.setTypeIds(typeIds);
        blog.setTypes(types);
        return blog;
    }

    /**
     * 查询文章管理列表
     *
     * @param cmsBlog 文章管理
     * @return 文章管理
     */
    @Override
    public List<CmsBlog> selectCmsBlogList(CmsBlog cmsBlog)
    {
        List<CmsBlog> cmsBlogList = cmsBlogMapper.selectCmsBlogList(cmsBlog);
        return BlogListAddTypeAndTag(cmsBlogList);
    }

    /**
     * 查询推荐文章列表
     */
    @Override
    public List<CmsBlog> selectCmsBlogListRecommend(CmsBlog cmsBlog) {
        List<CmsBlog> cmsBlogList = cmsBlogMapper.selectCmsBlogListRecommend(cmsBlog);
        List<CmsBlog> blogList = BlogListAddTypeAndTag(cmsBlogList);
        return blogList;
    }

    /**
     * 按分类查询文章列表
     */
    @Override
    public List<CmsBlog> selectCmsBlogListByTypeId(Long id) {
        List<CmsBlog> cmsBlogList = cmsBlogMapper.selectCmsBlogListByTypeId(id);
        List<CmsBlog> blogList = BlogListAddTypeAndTag(cmsBlogList);
        return blogList;
    }

    @Override
    public List<CmsBlog> selectCmsBlogListByTypeIdStep1(Long id) {
        return cmsBlogMapper.selectCmsBlogListByTypeId(id);
    }

    @Override
    public List<CmsBlog> selectCmsBlogListByTypeIdStep2(List<CmsBlog> cmsBlogList) {
        return BlogListAddTypeAndTag(cmsBlogList);
    }

    /**
     * 按标签查询文章列表
     */
    @Override
    public List<CmsBlog> selectCmsBlogListByTagId(Long id) {
        List<CmsBlog> cmsBlogList = cmsBlogMapper.selectCmsBlogListByTagId(id);
        return BlogListAddTypeAndTag(cmsBlogList);
    }

    @Override
    public List<CmsBlog> selectCmsBlogListByTagIdStep1(Long id) {
        return cmsBlogMapper.selectCmsBlogListByTagId(id);
    }

    @Override
    public List<CmsBlog> selectCmsBlogListByTagIdStep2(List<CmsBlog> cmsBlogList) {
        return BlogListAddTypeAndTag(cmsBlogList);
    }

    private String setStudentInfo(CmsBlog cmsBlog) {
        int studentId = cmsBlog.getStudentId();
        TeachStudent student = teachStudentMapper.selectTeachStudentByStudentId((long) studentId);
        return student.getSex();
    }

    /**
     * 新增文章管理
     *
     * @param cmsBlog 文章管理
     * @return 结果
     */
    @Override
    public Long insertCmsBlog(CmsBlog cmsBlog)
    {
        cmsBlog.setCreateTime(DateUtils.getNowDate());
//        String sex = setStudentInfo(cmsBlog);
//        cmsBlog.setStudentSex(sex);
        cmsBlogMapper.insertCmsBlog(cmsBlog);
        Long blogId = cmsBlog.getId();
        //新增文章标签
        Long[] tagIds = cmsBlog.getTagIds();
        if (tagIds!=null&&tagIds.length>0){
            List<CmsBlogTag> blogTagList = new ArrayList<>();
            for (Long tagId : tagIds) {
                CmsBlogTag cmsBlogTag = new CmsBlogTag();
                cmsBlogTag.setBlogId(blogId);
                cmsBlogTag.setTagId(tagId);
                blogTagList.add(cmsBlogTag);
            }
            cmsBlogTagMapper.batchBlogTag(blogTagList);
        }
        //新增文章分类
        Long[] typeIds = cmsBlog.getTypeIds();
        if (typeIds!=null&&typeIds.length>0){
            List<CmsBlogType> blogTypeList = new ArrayList<>();
            for (Long typeId : typeIds) {
                CmsBlogType cmsBlogType = new CmsBlogType();
                cmsBlogType.setBlogId(blogId);
                cmsBlogType.setTypeId(typeId);
                blogTypeList.add(cmsBlogType);
            }
            cmsBlogTypeMapper.batchBlogType(blogTypeList);
        }
        return blogId;
    }

    /**
     * 得到学生的头像信息
     *
     * @param cmsBlog cmsBlog
     * @return String
     */
    @Override
    public String getStudentAvatar(CmsBlog cmsBlog) {
        int studentId = cmsBlog.getStudentId();
        if (studentId == 0){
            return "";
        }
        TeachStudent teachStudent = teachStudentMapper.selectTeachStudentByStudentId((long) studentId);
        if (teachStudent == null){
            return "";
        }
        else {
            String avatar = teachStudent.getAvatar();
            return StringUtils.getStringAvatar(avatar);
        }
    }

    @Override
    public int updateCmsBlogByStudentId(CmsBlog cmsBlog)
    {
        return cmsBlogMapper.updateCmsBlogByStudentId(cmsBlog);
    }

    /**
     * 修改文章管理
     *
     * @param cmsBlog 文章管理
     * @return 结果
     */
    @Override
    public int updateCmsBlog(CmsBlog cmsBlog)
    {
        cmsBlog.setUpdateTime(DateUtils.getNowDate());
        Long blogId = cmsBlog.getId();
        //清空文章分类关联
        cmsBlogTypeMapper.deleteBlogTypeByBlogId(blogId);
        //清空文章标签关联
        cmsBlogTagMapper.deleteBlogTagByBlogId(blogId);
        //新增文章标签
        Long[] tagIds = cmsBlog.getTagIds();
        if (tagIds!=null&&tagIds.length>0){
            List<CmsBlogTag> blogTagList = new ArrayList<>();
            for (Long tagId : tagIds) {
                CmsBlogTag cmsBlogTag = new CmsBlogTag();
                cmsBlogTag.setBlogId(blogId);
                cmsBlogTag.setTagId(tagId);
                blogTagList.add(cmsBlogTag);
            }
            cmsBlogTagMapper.batchBlogTag(blogTagList);
        }
        //新增文章分类
        Long[] typeIds = cmsBlog.getTypeIds();
        if (typeIds!=null&&typeIds.length>0){
            List<CmsBlogType> blogTypeList = new ArrayList<>();
            for (Long typeId : typeIds) {
                CmsBlogType cmsBlogType = new CmsBlogType();
                cmsBlogType.setBlogId(blogId);
                cmsBlogType.setTypeId(typeId);
                blogTypeList.add(cmsBlogType);
            }
            cmsBlogTypeMapper.batchBlogType(blogTypeList);
        }
        return cmsBlogMapper.updateCmsBlog(cmsBlog);
    }

    @Override
    public int updateCmsState(int id, int type) {
        return cmsBlogMapper.updateEventFinishState2(id,type);
    }

    @Override
    public int updateCmsState(CmsBlog cmsBlog) {
        if (cmsBlog.getId() == null || cmsBlog.getId().intValue() == 0){
            log.info("未能识别出需要更新的ID");
            return 0;
        }
        LoginUser loginUser = SecurityUtils.getLoginUser();
        SysUser user = loginUser.getUser();
        Long LoginId = 0L;
        if (user != null) {
            Long userId = user.getUserId();
            cmsBlog.setFinishBy(userId);
        }
        if (cmsBlog.getFinishState() == Constants.CMS_STATE_FINISH){
            cmsBlog.setFinishTime(DateUtils.getNowDate());
        }
        log.info("---获取的登录人的ID:{}",LoginId);
        return cmsBlogMapper.updateCmsBlog(cmsBlog);
    }

    @Override
    public int updateCmsState(String id) {
        return cmsBlogMapper.updateEventFinishState(id);
    }

    /**
     * 批量删除文章管理
     *
     * @param ids 需要删除的文章管理主键
     * @return 结果
     */
    @Override
    public int deleteCmsBlogByIds(Long[] ids)
    {
        for (Long id : ids) {
            //清空文章分类关联
            cmsBlogTypeMapper.deleteBlogTypeByBlogId(id);
            //清空文章标签关联
            cmsBlogTagMapper.deleteBlogTagByBlogId(id);
        }
        return cmsBlogMapper.deleteCmsBlogByIds(ids);
    }

    @Override
    public int deleteCmsByIds(Long[] ids)
    {
        for (Long id : ids) {
            //清空文章分类关联
            cmsBlogTypeMapper.deleteBlogTypeByBlogId(id);
            //清空文章标签关联
            cmsBlogTagMapper.deleteBlogTagByBlogId(id);
        }
        return cmsBlogMapper.deleteCmsByIds(ids);
    }

    /**
     * 删除文章管理信息
     *
     * @param id 文章管理主键
     * @return 结果
     */
    @Override
    public int deleteCmsBlogById(Long id)
    {
        //清空文章分类关联
        cmsBlogTypeMapper.deleteBlogTypeByBlogId(id);
        //清空文章标签关联
        cmsBlogTagMapper.deleteBlogTagByBlogId(id);
        return cmsBlogMapper.deleteCmsBlogById(id);
    }

    @Override
    public int deleteCmsById(Long id)
    {
        //清空文章分类关联
        cmsBlogTypeMapper.deleteBlogTypeByBlogId(id);
        //清空文章标签关联
        cmsBlogTagMapper.deleteBlogTagByBlogId(id);
        return cmsBlogMapper.deleteCmsById(id);
    }

    private List<CmsBlog> BlogListAddTypeAndTag(List<CmsBlog> cmsBlogList){
        if (cmsBlogList == null){
            return null;
        }
        for (CmsBlog blog : cmsBlogList) {
            Long blogId = blog.getId();
            //查询标签列表
//            List<CmsBlogTag> blogTagList = cmsBlogTagMapper.selectBlogTagList(blogId);
//            List<CmsTag> cmsTagList = new ArrayList<>();
//            blogTagList.forEach((CmsBlogTag cmsBlogTag)->{
//                Long tagId = cmsBlogTag.getTagId();
//                CmsTag cmsTag = cmsTagMapper.selectCmsTagByTagId(tagId);
//                cmsTagList.add(cmsTag);
//            });
//            blog.setTags(cmsTagList);

            //查询分类列表
//            List<CmsBlogType> blogTypeList = cmsBlogTypeMapper.selectBlogTypeList(blogId);
//            List<CmsType> cmsTypeList = new ArrayList<>();
//            blogTypeList.forEach((CmsBlogType cmsBlogType)->{
//                Long typeId = cmsBlogType.getTypeId();
//                CmsType cmsType = cmsTypeMapper.selectCmsTypeByTypeId(typeId);
//                cmsTypeList.add(cmsType);
//            });
//            blog.setTypes(cmsTypeList);

            //查询学生标签列表
//            List<TeachStudentTag> teachStudentTags = teachStudentTagService.selectTeachStudentTagStudentId((long) blog.getStudentId());
//            List<String> studentTags = new ArrayList<>();
//            teachStudentTags.forEach((teachStudentTag)->{
//                String tagName = teachStudentTag.getTagName();
//                studentTags.add(tagName);
//            });
//            blog.setStudentTags(studentTags);

            //查询评价列表
            List<CmsComment> commentList = commentService.selectCommentList(blogId);
            List<String> commentLists = new ArrayList<>();
            commentList.forEach((comment)->{
                String content = comment.getContent();
                commentLists.add(comment.getCreateBy() + ":" + content);
            });
            blog.setCommentLists(commentLists);
        }
        return cmsBlogList;
    }
}
